﻿#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "jthread.h"
#include "jfuturewatcher.h"
#include "jconcurrentmap.h"
#include <QMap>

// class ThreadDialog

class QSpinBox;
class QComboBox;
class QPushButton;
class QLineEdit;
class QProgressBar;

//
typedef QPair<QString, QMap<QString, int> > WordCount;

class ThreadDialog : public QDialog, public JFutureWatcherBaseObserver
{
    Q_OBJECT
public:
    explicit ThreadDialog(QWidget *parent = 0);
    ~ThreadDialog();

    // JFutureWatcherBaseObserver interface
public:
    virtual void started();
    virtual void finished();
    virtual void canceled();
    virtual void paused();
    virtual void resumed();
    virtual void resultReadyAt(int resultIndex);
    virtual void resultsReadyAt(int beginIndex, int endIndex);
    virtual void progressRangeChanged(int minimum, int maximum);
    virtual void progressValueChanged(int progressValue);
    virtual void progressTextChanged(const std::string &progressText);

public Q_SLOTS:
    void onThreadCountChanged(int value);
    void onButtonStartClicked();
    void onButtonPauseClicked();
    void onButtonCancelClicked();
    void onTimeout();

private:
    static QStringList findFiles(const QString &startDir, const QStringList &filters);
    static WordCount countWords(const QString &file);
    static void reduce(WordCount &result, const WordCount &w);

private:
    QSpinBox *q_spinBoxThreadCount;
    QComboBox *q_comboBoxDistributeType;
    QPushButton *q_buttonStart;
    QPushButton *q_buttonPause;
    QPushButton *q_buttonCancel;
    QLineEdit *q_lineEditActivateCount;
    QProgressBar *q_progressBarTask;
    QLineEdit *q_lineEditTaskStatus;
    JFutureWatcher<WordCount> q_futureWatcher;
};

#endif // DIALOG_H
